<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\addGoodsType;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\Common\ajaxReturnJsonContent;

class CommodityTypeController extends Controller
{
    /**
     * 分类列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询出顶级分类
        $res = DB::table('shop_goodsType')->select('id', 'name', 'path')
                                          ->where('path', '0,')
                                          ->get('5');
        return view('admin.goodsType',['res' => $res]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 添加分类.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(addGoodsType $request)
    {
        //获取传过来的数据
        $name = $request->input('name');
        $pid = $request->input('pid', 0);
        $path = $request->input('path', '0,');

        //判断pid是否为0
        if ($pid != 0) {
            //不为0代表不是1级分类，则拼接好path
            $path .= $pid.',';
        }

        //添加数据
        $insertId = DB::table('shop_goodsType')->insertGetId([
            'name' => $name,
            'pid' => $pid,
            'path' => $path
        ]);
        //获取返回的数据
        $result = ajaxReturnJsonContent::returnVal($insertId, '添加成功', '添加失败');
        return redirect('admin/CommodityType')->with($result);
    }

    /**
     * 根据传过来的path查询子分类
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $id)
    {
        if (!$request->ajax()) {
            return View('admin/LogOut');
        }
        //获取子分类数据，$id为子分类的path
        $res = DB::table('shop_goodsType')->select('id', 'name', 'path')
                                          ->where('path', $id)
                                          ->get();
        //获取返回的数据
        $res = ajaxReturnJsonContent::returnVal($res, 'success', 'fail');
        return json_encode($res);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //获取传过来的name值
        $res['name'] = $request->input('name');

        //根据过来的$id修改分类的名称
        $bool = DB::table('shop_goodsType')->where('id', $id)
                                           ->update($res);

        $result = ajaxReturnJsonContent::returnVal($bool, '修改成功', '修改失败');
        return redirect('admin/CommodityType')->with($result);
    }

    /**
     * 删除数据.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //根据传过来的id查询是否还有字分类
        $SubClass = DB::table('shop_goodsType')->select('id')
                                   ->where('pid', $id)
                                   ->first();
        //判断是否有子分类数据，有则走进true区间
        if ($SubClass) {
            $bool = false;
            $result = ajaxReturnJsonContent::returnVal($bool, '', '还有子分类存在，无法删除');
            return redirect('admin/CommodityType')->with($result);
        }

        //判断分类是否还有商品
        $classShop = DB::table('shop_goodsInfo')->select('id')
                                            ->where('tid', $id)
                                            ->first();
        //判断改分类是否有商品数据，有则走进true区间
        if ($classShop) {
            $bool = false;
            $result = ajaxReturnJsonContent::returnVal($bool, '', '改分类还有商品存在，无法删除');
            return redirect('admin/CommodityType')->with($result);
        }


        //删除数据
        $res = DB::table('shop_goodsType')->where('id',$id)
                                          ->delete();
        $result = ajaxReturnJsonContent::returnVal($res, '删除成功', '删除失败');
        return redirect('admin/CommodityType')->with($result);
    }
}
